package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessDetailMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.yw.YwContractsRegisterMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.ibiz.yw.IYwContractsRegisterService;
import com.zjdiepu.www.model.yw.dto.YwContractsRegister;
import com.zjdiepu.www.model.yw.ext.YwContractsRegisterExt;

/**
 * 合同登记Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月04日 17时08分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwContractsRegisterServiceImpl extends BaseService implements IYwContractsRegisterService {
	public static final Logger logger = LoggerFactory.getLogger(YwContractsRegisterServiceImpl.class);

	@Autowired
	private IFkAuditListService fkAuditListService;
	@Autowired
	private YwContractsRegisterMapper ywContractsRegisterMapper;
	@Resource
	private YwBusinessMapper ywBusinessMapper;
	@Resource
	private YwBusinessDetailMapper ywBusinessDetailMapper;
	@Resource
	private FkAuditListMapper fkAuditListMapper;

	
	@Override
	public void saveUpdateAndDelete(YwContractsRegister ywContractsRegister, OperateType operType) {
		switch (operType) {
		case add:
			ywContractsRegister.initInsertData(ywContractsRegister.getModifier(), ywContractsRegister.getModifierName());
			ywContractsRegisterMapper.insert(ywContractsRegister);
			break;
		case edit:
			ywContractsRegister.initUpdateData(ywContractsRegister.getModifier(), ywContractsRegister.getModifierName());
			// 影响的记录数
			int updateCount = ywContractsRegisterMapper.updateByPrimaryKeySelective(ywContractsRegister);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywContractsRegister.initDeleteData(ywContractsRegister.getModifier(), ywContractsRegister.getModifierName());
			// 影响的记录数
			int deleteCount = ywContractsRegisterMapper.updateByPrimaryKeySelective(ywContractsRegister);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwContractsRegister> selectByExt(YwContractsRegisterExt ywContractsRegisterExt) {
		return ywContractsRegisterMapper.selectByExt(ywContractsRegisterExt);
	}
	
	@Override
	public int countByExt(YwContractsRegisterExt ywContractsRegisterExt){
		return ywContractsRegisterMapper.countByExt(ywContractsRegisterExt);
	}
	
	@Override
	public YwContractsRegister selectById(String id) {
		return ywContractsRegisterMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywContractsRegisterMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwContractsRegister ywContractsRegister) {
		return ywContractsRegisterMapper.countRefined(ywContractsRegister);
	}

	@Override
	public YwContractsRegister selectRefined(YwContractsRegister ywContractsRegister) {
		return ywContractsRegisterMapper.selectRefined(ywContractsRegister);
	}
	
	@Override
	public int deletedRefined(YwContractsRegister ywContractsRegister) {
		return ywContractsRegisterMapper.deletedRefined(ywContractsRegister);
	}

	@Override
	public boolean repeat(YwContractsRegister ywContractsRegister) {
		int count = countRefined(ywContractsRegister);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywContractsRegister.getId())) {
			return true;
		}
		YwContractsRegister dbItem = selectRefined(ywContractsRegister);
		if(null == dbItem) {
			return false;
		}
		return !ywContractsRegister.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywContractsRegisterMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwContractsRegister> ywContractsRegisters) {
		return ywContractsRegisterMapper.insertBatch(ywContractsRegisters);
	}

	@Override
	public void save(YwContractsRegister ywContractsRegister,
			AbstractSysUser currentUser) {
    	ywContractsRegister.initInsertData(currentUser.getId(), currentUser.getName());
    	ywContractsRegisterMapper.insert(ywContractsRegister);
		
        
    	//xiongxiaotun 删除状态修改
//        String busiId = ywContractsRegister.getBusiId();
//        List<String> ids = new ArrayList<String>();
//        ids.add(busiId);
//        YwBusiness ywBusiness = new YwBusiness();
//        ywBusiness.setBusiStatus(BusiStatus.ZI_SHEN_YI_SHEN_ZHONG.getCode());
//        ywBusinessMapper.updateCommonBatch(ywBusiness, ids);
	}
}
